package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.entity.User;
import com.example.service.UserService;
import com.example.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "用户管理") //swagger的注释
@CrossOrigin
@RequestMapping("/user")
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "查询单个用户")
    @GetMapping(value = "selectOne/{userId}")
    public R selectOne(@PathVariable String userId) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        User user = userService.getOne(wrapper);
        return R.ok().data("user", user);
    }

    @ApiOperation(value = "分页查询用户")
    @GetMapping("selectUser/{current}/{limit}")
    public R selectUser(
            @ApiParam(name = "current", value = "当前页", required = true) @PathVariable long current,
            @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable long limit
    ) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("gmt_create");
        //实现分页
        Page<User> userPage = new Page<>(current, limit);
        userService.page(userPage, wrapper);
        //获取分页后数据
        long total = userPage.getTotal();
        List<User> list = userPage.getRecords();
        return R.ok().data("total", total).data("list", list);
    }

    @ApiOperation(value = "封禁用户")
    @GetMapping("deleteUser/{id}")
    public R deleteUser(@PathVariable String id) {
        User user = new User();
        user.setIsDeleted(1);
        user.setUserId(id);
        boolean b = userService.updateById(user);
        if (b) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @ApiOperation(value = "启用用户")
    @GetMapping("repeatUser/{id}")
    public R repeatUser(@PathVariable String id) {
        User user = new User();
        user.setIsDeleted(0);
        user.setUserId(id);
        boolean b = userService.updateById(user);
        if (b) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @ApiOperation(value = "条件分页查询用户")
    @PostMapping("selectUser/{current}/{limit}")
    public R selectUserCondition(
            @ApiParam(name = "current", value = "当前页", required = true) @PathVariable long current,
            @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable long limit,
            @RequestBody(required = false) User user
    ) {
        Page<User> pageUser = new Page<>(current, limit);
        QueryWrapper wrapper = new QueryWrapper();
        String userId = user.getUserId();
        String name = user.getName();
        String phone = user.getPhone();
        Integer level = user.getLevel();

        if (!StringUtils.isEmpty(userId)) {
            wrapper.eq("user_id", userId);
        }
        if (!StringUtils.isEmpty(name)) {
            wrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(phone)) {

            wrapper.like("phone", phone);
        }
        if (!StringUtils.isEmpty(level)) {

            wrapper.eq("level", level);
        }
        //排序
        wrapper.orderByDesc("gmt_create");

        IPage page = userService.page(pageUser, wrapper);
        //获取分页后数据
        long total = page.getTotal();
        List<User> list = page.getRecords();
        return R.ok().data("total", total).data("list", list);
    }

    @ApiOperation(value = "批量增加用户")
    @PostMapping("addUser")
    public R addUser(@RequestBody List<User> userList) {
        boolean b = userService.saveBatch(userList);
        if (b) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @ApiOperation(value = "修改用户")
    @PostMapping("updateUser")
    public R updateUser(@RequestBody User user) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", user.getUserId());
        boolean b = userService.update(user, wrapper);
        if (b) {
            return R.ok();
        } else {
            return R.error();
        }
    }


}
